//################################
// 版权所有：中国农业银行软件开发中心
// 系统名称：移动办公应用-手机版
// 文件名称：childAttachment.js
// 创建时间：2016年09月01日
// 创建人员：Panda
// 功能说明：附件组件子组件
//################################

'use strict';

import React, {Component, PropTypes} from 'react';
import {
    StyleSheet,
    Image,
    ScrollView,
    Text,
    View,
    TouchableOpacity,
    InteractionManager,
} from 'react-native';

import Colors from '../../common/colors';
import CommonFunc from '../../common/commonFunc';

import Icon from 'react-native-vector-icons/FontAwesome';

const AttachmentFileType = [
    'zip',
    'doc',
    'docx',
    'xls',
    'xlsx',
    'ppt',
    'pptx',
    'jpg',
    'jpeg',
    'png',
    'bmp',
    'wps',
    'et',
    'dps',
    'pdf',
    'gif',
];

const ICON_SIZE = 28;

class ChildAttachment extends Component {
    static propTypes = {
		attachment: PropTypes.object.isRequired,
        onPress: PropTypes.func.isRequired,
	}

    static defaultProps = {
		attachment: {},
        onPress: () => null,
    }

    constructor(props) {
        super(props);

        this.attachment = props.attachment;
        this.init();
    }

    init() {
        this.iconName = this.getIconName();
        this.fileName = decodeURIComponent(this.attachment.fileName);
        this.fileSize = this.getFileSize();
    }

    getFileSize() {
        let _fileSize = CommonFunc.getFileSizeStr(parseInt(this.attachment.fileSize));
        if (_fileSize && _fileSize.length && _fileSize.length > 0) {
            _fileSize = '(' + _fileSize + ')';
        }
        return _fileSize;
    }

    getIconName() {
        let suffix = this.attachment.fileName;
        suffix = suffix.substring(suffix.lastIndexOf(".") + 1, suffix.length);

        let iconName = 'file-text-o';//默认的附件图标

        switch (suffix) {
            case 'zip':
                iconName = 'archive';
                break;
            case 'doc':
                iconName = 'file-word-o';
                break;
            case 'docx':
                iconName = 'file-word-o';
                break;
            case 'xls':
                iconName = 'file-excel-o';
                break;
            case 'xlsx':
                iconName = 'file-excel-o';
                break;
            case 'ppt':
                iconName = 'file-powerpoint-o';
                break;
            case 'pptx':
                iconName = 'file-powerpoint-o';
                break;
            case 'jpg':
                iconName = 'file-image-o';
                break;
            case 'jpeg':
                iconName = 'file-image-o';
                break;
            case 'png':
                iconName = 'file-image-o';
                break;
            case 'bmp':
                iconName = 'file-image-o';
                break;
            case 'wps':
                iconName = 'file-word-o';
                break;
            case 'et':
                iconName = 'file-excel-o';
                break;
            case 'dps':
                iconName = 'file-powerpoint-o';
                break;
            case 'pdf':
                iconName = 'file-pdf-o';
                break;
            case 'gif':
                iconName = 'file-image-o';
                break;
            default:
                break;
        }

        return iconName;
    }

    _onPress() {
        InteractionManager.runAfterInteractions(() => {
            if (this.fileName.lastIndexOf('.zip') > 0) {
                alert('not support');
            }
            else {
                if (this.props && this.props.onPress) {
                    this.props.onPress(this.attachment);
                }
            }
        });
	}

    render() {
        return (
            <View style={styles.view}>
                <TouchableOpacity style={styles.container} onPress={this._onPress.bind(this)}>
                    <Icon style={styles.attachmenticon} name={this.iconName} size={ICON_SIZE} color={Colors.attachmentIcon} />
                    <ScrollView>
                        <Text style={styles.attachmentnametext}>{this.fileName + '  ' + this.fileSize}</Text>
                    </ScrollView>
                </TouchableOpacity>
            </View>
        );
    }
}

const styles = StyleSheet.create({
    view: {
        flex: 1,
    },
    container: {
        flex:1,
        flexDirection: 'row',
        paddingLeft: 12,
        paddingRight: 12,
        alignItems: 'center',
        marginTop: 8,
        marginBottom: 8,
        marginLeft: 40,
    },
    attachmenticon: {
        width: 30,
        height: 30,
    },
    attachmentnametext: {
        flex: 1,
        marginLeft: 8,
        color: Colors.attachmentFileName,
    },
});

export default ChildAttachment;